local modname = minetest.get_current_modname()
local modpath = minetest.get_modpath(modname)

local element='dirt'
local element_is_enabled = minetest.settings:get_bool('element_portals_enable_'..element..'_portals', true)
if not element_is_enabled then return end

-- Intllib
local S, NS = dofile(modpath.."/intllib.lua")

local range = minetest.settings:get('element_portals_'..element..'_portals_range') or 25
local portal_out = modname..":portal_out"
local portal_inactive = modname..":portal"
local portal_active = modname..":portal_active"

-- Tiles 

local portal_tiles ={}
portal_tiles.generic = {
	side = "dirt_portals_side.png",
	face = "dirt_portals_face.png",
	back = "dirt_portals_face.png",
		}
portal_tiles.active = {
	--	top = {name="sand_portal_anim.png", animation={type="vertical_frames", aspect_w=16, aspect_h=16, length=1.0}},
	top = portal_tiles.generic.side,
	bottom = portal_tiles.generic.side.."^[transformR180]",
	left = portal_tiles.generic.side.."^[transformR90]",
	right = portal_tiles.generic.side.."^[transformR270]",
	front = portal_tiles.generic.face,
	back = portal_tiles.generic.back,
	}
portal_tiles.inactive = {
	top = portal_tiles.generic.side,
	bottom = portal_tiles.generic.side.."^[transformR180]",
	left = portal_tiles.generic.side.."^[transformR90]",
	right = portal_tiles.generic.side.."^[transformR270]",
	front = portal_tiles.generic.face,
	back = portal_tiles.generic.back,
	}


local find_target = function(pos,node)
	local p2 = node.param2
	local dir = minetest.facedir_to_dir(p2)
	local node_name = node.name
	local p = pos	
	-- Find target or obstacle
	for i = 1,range do
		-- local p = increment_pos_from_dir(pos,p2,i)
		p = {
			x = p.x + dir.x,
			y = p.y + dir.y,
			z = p.z + dir.z,
			}

		local n = minetest.get_node(p).name

		if n == portal_out then

			-- local meta = minetest.get_meta(pos)
			if node_name ~= portal_active then
				minetest.set_node(pos,{name=portal_active, param2=p2})
			end
			-- meta:set_string('target',minetest.pos_to_string(p))	
			return p

--		elseif n ~= "air" and n ~= portal_vortex then
		elseif minetest.get_item_group(n,'soil') < 1 then

			if node_name ~= portal_inactive then
				minetest.set_node(pos,{name=portal_inactive, param2=p2})
			end
			return false
		end
	end

	-- Nothing found
	if node_name ~= portal_inactive then
		minetest.set_node(pos,{name=portal_inactive, param2=p2})
	end
	return false
end

local portal_action = function(pos, node, player) --, itemstack)
		
	-- -- -- -- -- -- -- -- -- --
	-- Update target		-- --	
	-- -- -- -- -- -- -- -- -- --
	local target = find_target(pos, node)
	if node.name ~= portal_active or not target then return end

	-- -- -- -- -- -- -- -- -- -- -- -- --
	-- Find and teleport player		 -- --	
	-- -- -- -- -- -- -- -- -- -- -- -- --

	-- local above = {x=pos.x,y=pos.y+1,z=pos.z}
	if target then
			-- force emerge of target area
			minetest.get_voxel_manip():read_from_map(target, target)
		if not minetest.get_node_or_nil(target) then
		minetest.emerge_area(
		vector.subtract(target, 4), vector.add(target, 4))
		end
			-- teleport the player
		minetest.after(0.1, function(player, pos, target)
			local pchpos = player:getpos()
			-- pchpos.y = pchpos.y + 0.1
			target.y = target.y + 1
								
			player:setpos(target)

			end, player, pos, target)
	end
	--return itemstack
end

minetest.register_node(portal_inactive, {
	description = S("Portal"),
	-- tiles = {+Y, -Y, +X, -X, +Z, -Z},
	tiles = {
		portal_tiles.inactive.top,
		portal_tiles.inactive.bottom,
		portal_tiles.inactive.right,
		portal_tiles.inactive.left,
		portal_tiles.inactive.front,
		portal_tiles.inactive.back,
		},
	paramtype = "light",
	paramtype2 = "facedir",
	legacy_facedir_simple = true,
	is_ground_content = false,
	groups = {crumbly=3},
	on_rightclick = function(pos, node, clicker, itemstack)
		-- Nothing happen
	end
})

minetest.register_node(portal_active, {
	description = S("Portal"),
	-- tiles = {+Y, -Y, +X, -X, +Z, -Z},
	tiles = {
		portal_tiles.active.top,
		portal_tiles.active.bottom,
		portal_tiles.active.right,
		portal_tiles.active.left,
		portal_tiles.active.front,
		portal_tiles.active.back,
		},
	paramtype = "light",
	paramtype2 = "facedir",
	light_source = 2,
	is_ground_content = false,
	legacy_facedir_simple = true,
	drop = portal_inactive,
	groups = {crumbly=3, not_in_creative_inventory = 1 },
	on_rightclick = portal_action
	-- on_punch = portal_action
})

minetest.register_node(portal_out, {
	description = S("Out Portal"),
	-- tiles = {+Y, -Y, +X, -X, +Z, -Z},
	tiles = {portal_tiles.generic.face},
	paramtype = "light",
	paramtype2 = "facedir",
	is_ground_content = false,
	groups = {crumbly=3},
})

minetest.register_abm({
	nodenames = {portal_inactive,portal_active},
	interval = 1,
	chance = 2,
	action = function(pos, node)

		-- -- -- -- -- -- -- -- -- --
		-- Update target		-- --	
		-- -- -- -- -- -- -- -- -- --
		local target = find_target(pos, node)

	end,
}) --]]

minetest.register_craft({
	output = portal_inactive,
	recipe = {
		{'default:dirt', 'default:dirt', 'default:dirt'},
		{'default:dirt', 'default:mese_crystal', 'default:dirt'},
		{'default:dirt', 'default:dirt', 'default:dirt'}
	}
})

minetest.register_craft({
	output = portal_out,
	recipe = {
		{'default:dirt', 'default:dirt', 'default:dirt'},
		{'default:dirt', 'default:flint', 'default:dirt'},
		{'default:dirt', 'default:dirt', 'default:dirt'}
	}
})
